package threadTest;

public class MoreTreads {
	public static void main(String[] args) {
		class Object {
			private String a;
			private String b;
			public String getA() {
				return a;
			}
			public void setA(String a) {
				this.a = a;
			}
			public String getB() {
				return b;
			}
			public void setB(String b) {
				this.b = b;
			}
		}
		
		Object o = new Object();
		o.setA("beginA:");
		o.setB("beginB:");
		
		class ThreadA implements Runnable{
			private Object o;
			public ThreadA(Object o){
				this.o = o;
			}
			public void run(){
				synchronized (o) {
					System.out.println("-----in A-----");
					for(int i=0;i<10;i++){
						o.setA(o.getA()+i);
						System.out.println("Thread A=="+o.getA());
						if(i == 5){
							try {
								//i++;
								o.wait();
								System.out.println("after wait A:"+o.getA());
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		};
		
		class ThreadB implements Runnable{
			private Object o;
			public ThreadB(Object o){
				this.o = o;
			}
			public void run(){
				synchronized (o) {
					System.out.println("-----in B-----");
					for(int i=0;i<10;i++){
						o.setB(o.getB()+i);
						System.out.println("Thread B=="+o.getB());
						if(i == 5){
							try {
								//i++;
								o.wait();
								System.out.println("after wait B:"+o.getB());
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		};
		
		class ThreadS implements Runnable{
			private Object o;
			public ThreadS(Object o){
				this.o = o;
			}
			public void run(){
				synchronized (o) {
					System.out.println("------in S-----");
					o.setA(o.getA()+"|");
					o.setB(o.getB()+"|");
					o.notifyAll();
					System.out.println("------out S-----");
				}
			}
		};
		
		Thread t1 = new Thread(new ThreadA(o));
//		t1.setDaemon(true);
		t1.setPriority(1);
		Thread t2 = new Thread(new ThreadB(o));
		t2.setDaemon(true);
		t2.setPriority(5);
		Thread t3 = new Thread(new ThreadS(o));
		t3.setPriority(10);
		
		System.out.println("start!!!!");
		t1.start();
		t2.start();
		t3.start();
		System.out.println("end!!!!");//不一定在哪出现
	}
}
